package com.woniuxy.controller;

import com.woniuxy.entity.Product;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * @Author: 吴秋达
 * @Do: TODO
 * @DateTime: 2022年12月5日 18:53:00
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("/product")
public class ProductController {
    @Value("${server.port}")
    int port;
    @Autowired
    ProductService productService;

    @RequestMapping("/{pid}")
    public ResponseEntity findOneById(@PathVariable Integer pid) {
        ResponseEntity oneById = productService.findOneById(pid);
        oneById.put("port", port);
        return oneById;
    }

    @PutMapping
    public ResponseEntity updateProduct(@RequestBody Product product) {
        return productService.update(product);
    }

    /**
     * 通过名字或者价格的范围，查询商品列表
     * @param name
     * @param minPrice
     * @param maxPrice
     * @return com.woniuxy.entity.utils.ResponseEntity
     */
    @GetMapping("/findCondition")
    public ResponseEntity findAllByNameOrPrice(String name,double minPrice,double maxPrice){
        Random random = new Random();
        int i = random.nextInt(3-1+1)+1;
        if (i != 3) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return productService.findAllByNameOrPrice(name,minPrice,maxPrice);
    }
}
